﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainController.Hardware.PLC.MEGMEET.Datas
{
    public enum CommAction
    {
        None = 0,
        ReadMultReg,//读多个通用寄存器

        WriteSingleReg,//写单通用寄存器
        WriteMultReg,//写多个通用寄存器

        ReadInput,//读输入寄存器
        ReadOutput,//读输出寄存器
    }

    class CommUnit 
    {
        public CommAction Action;
        public int DataAddrHead = 0;
        public ushort[] Data = null;

        public CommUnit(CommAction action, int addr, ushort[] data)
        {
            Action = action;
            DataAddrHead = addr;
            Data = data;
        }

        public CommUnit(CommAction action, int addr, int[] data)
        {
            Action = action;
            DataAddrHead = addr;

            for (int i = 0; i < data.Length; i++)
            {
                data[addr + i * 2] = (ushort)(data[i] & ushort.MaxValue);
                data[addr + i * 2 + 1] = (ushort)(data[i] >> 16);
            }

        }

        public CommUnit Clone()
        {
            return new CommUnit(Action, DataAddrHead, Data == null ? null : (ushort[])Data.Clone());
        }
    }
}
